package com.example.administrator.u17.activity;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

import com.example.administrator.u17.R;
import com.example.administrator.u17.adapter.SearchTopDetailAdapter;
import com.example.administrator.u17.config.NetConfig;
import com.example.administrator.u17.modle.MyDbBean;
import com.example.administrator.u17.modle.SearchTopDetailBean;
import com.google.gson.Gson;

import org.xutils.DbManager;
import org.xutils.common.Callback;
import org.xutils.ex.DbException;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;

public class SelectedActivity extends AppCompatActivity {

    private ArrayList<SearchTopDetailBean.Comics> mData;
    private SearchTopDetailAdapter mAdapter;
    private ListView mListView;
    private String mPath;
    private DbManager mManager;
    private Button mButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_selected);
        mManager=x.getDb(new DbManager.DaoConfig());
        initView();
        initData();
        setData();
        setListener();
    }

    private void setListener() {
        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent=new Intent(SelectedActivity.this,ZhangJieActivity.class);
                intent.putExtra("id",mData.get(position).comicId);
                startActivity(intent);
            }
        });
    }

    private void setData() {
        mListView.setAdapter(mAdapter);
    }

    private void initData() {
        mData=new ArrayList<>();
        mAdapter=new SearchTopDetailAdapter(this,mData);
        Intent intent=getIntent();
        String name=intent.getStringExtra("name");
        try {
            String decode = URLDecoder.decode(name, "UTF-8");
            mButton.setText(decode);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

        mPath = NetConfig.SEARCH_INPUT_PATH+name;
        RequestParams params=new RequestParams(mPath);
        x.http().get(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                MyDbBean myDb=new MyDbBean();
                myDb.json=result;
                myDb.path=mPath;
                Calendar calendar=Calendar.getInstance();
                SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");
                myDb.time=format.format(calendar.getTime());
                try {
                    mManager.save(myDb);
                } catch (DbException e) {
                    e.printStackTrace();
                }

                SearchTopDetailBean bean=new Gson().fromJson(result,SearchTopDetailBean.class);
                mData.addAll(bean.data.returnData.comics);
                mAdapter.notifyDataSetChanged();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }

    private void initView() {
        mListView= (ListView) findViewById(R.id.lv_input_selected_activity);
        mButton= (Button) findViewById(R.id.btn_input_selected_activity);

    }

    public void onClick(View view) {
        switch (view.getId()){
            case R.id.iv_input_selected_activity:
                finish();
                break;
            case R.id.btn_input_selected_activity:
                Intent intent=new Intent(this,InputActivity.class);
                startActivity(intent);
                break;
        }
    }
}
